package com.jvm123.blog.service;

import com.jvm123.blog.entity.CollectPostRelationship;
import com.jvm123.blog.entity.Post;
import com.jvm123.blog.entity.Term;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yawn http://jvm123.com
 * 2019/10/25 9:52
 */
public class Jdk8Utils {
    public static boolean OpRepeatElement(Collection<CollectPostRelationship> collection,
                                          Consumer<CollectPostRelationship> consumer) {
        Map<String, List<CollectPostRelationship>> listMap = collection.stream().collect(Collectors.groupingBy((relate) -> {
            long postId = relate.getPostId();
            long collectId = relate.getCollectId();
            return collectId + "-" + postId;
        }));
        listMap.forEach((k, list) -> {
            for (int i = 0; i < list.size(); i++) {
                // 忽略第0个
                if (i == 0) continue;
                consumer.accept(list.get(i));
            }
        });
        return true;
    }

    public static List<Post> convert(List<Long> ids, Function<Long, Post> function) {
        return ids.stream().map(function).collect(Collectors.toList());
    }

    public static boolean OpDistinctElement(List<Term> list, Consumer<Term> consumer) {
        Map<String, List<Term>> listMap = list.stream().collect(Collectors.groupingBy((term) ->
            term.getTaxonomy() + term.getSlug()
        ));
        listMap.forEach((k, subList) -> consumer.accept(subList.get(0)));
        return true;
    }
}
